package com.shengwei.truck.driver.view.ui.mine.userinfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.lifecycle.Observer;

import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.bean.Result;
import com.shengwei.truck.driver.bean.TimeCount;
import com.shengwei.truck.driver.databinding.ActivityModifyRealnameBinding;
import com.shengwei.truck.driver.utils.CommonUtil;
import com.shengwei.truck.driver.view.ui.BaseActivity;
import com.shengwei.truck.driver.vm.PersonalInfoViewModel;
import java.lang.ref.WeakReference;
import static com.shengwei.truck.driver.view.ui.mine.userinfo.PersonalInfoActivity.MODIFY_NAME;
/**
 * 修改真实姓名、身份证
 */
public class ModifyNamIdActivity extends BaseActivity<PersonalInfoViewModel, ActivityModifyRealnameBinding> implements View.OnClickListener {
    private int mFromPage = 0;  //1姓名； 2身份证
    private String mPersonName;
    private String mIdCard;
    private String mTitle;
    private TimeCount mSmsTimeCount;
    private String mSmsCode;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        getLastIntent();
        this.setTitle(mTitle);
        super.onCreate(savedInstanceState);
        initView();
        initLiveDataObersve();
        mSmsTimeCount = new TimeCount(mDataBinding.btGetSmsCode,60*1000,1000);
        this.getLifecycle().addObserver(mSmsTimeCount);
    }
    /**
     * 创建liveData观察者，用于监听数据变化，从而更新UI
     */
    private void initLiveDataObersve() {

        //监听获取验证码成功
        mViewModel.getSmsLiveData().observe(this, new Observer<Result<String>>() {
            @Override
            public void onChanged(Result<String> result) {
                result.handler(new OnCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        if (mSmsTimeCount != null){
                            mSmsTimeCount.start();
                        }
                        Toast.makeText(ModifyNamIdActivity.this, R.string.smscode_ok,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        //更新成功时，监听更新用户信息接口返回的数据
        mViewModel.getUpdtUserLiveData().observe(this, new Observer<Result<String>>() {
            @Override
            public void onChanged(Result<String> result) {

                result.handler(new OnCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Intent i = new Intent();
                        if(!TextUtils.isEmpty(mPersonName)){
                            i.putExtra("name",mPersonName);
                        }
                        if(!TextUtils.isEmpty(mIdCard)){
                            i.putExtra("identify",mIdCard);
                        }
                        Toast.makeText(ModifyNamIdActivity.this,R.string.modify_ok,Toast.LENGTH_SHORT).show();
                        setResult(RESULT_OK,i);
                        finish();

                    }
                });
            }
        });
    }

    /**
     * 获取上页传递过来的数据
     */
    private void getLastIntent() {
        Intent i = this.getIntent();
        Bundle bundle = i.getExtras();
        if(bundle != null) {
            mFromPage = bundle.getInt("fromPage");
            mPersonName = bundle.getString("name");
            mIdCard = bundle.getString("idCard");
            mTitle = bundle.getString("title");
        }
    }

    private void initView() {
        if(mFromPage == MODIFY_NAME) {  //姓名
            mDataBinding.nameLayout.setVisibility(View.VISIBLE);
            if(!TextUtils.isEmpty(mPersonName)){
                mDataBinding.edtPersonName.setHint(R.string.please_input_name);
                mDataBinding.edtPersonName.setText(mPersonName);
            }
        }
        else{  //身份证
            mDataBinding.idCardLayout.setVisibility(View.VISIBLE);
            if(!TextUtils.isEmpty(mIdCard)){
                mDataBinding.edtIdentify.setHint(R.string.hint_input_identify);
                mDataBinding.edtIdentify.setText(mIdCard);
            }
        }
        mDataBinding.btSureModify.setOnClickListener(this);
        mDataBinding.btGetSmsCode.setOnClickListener(this);
    }
    @Override
    protected int getContentViewId() {
        return R.layout.activity_modify_realname;
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btSureModify) {
            if(mFromPage == MODIFY_NAME) {  //修改用户名
                updateName();
            }
            else{
                updateIdentify();
            }
        }
        else if(v.getId() == R.id.btGetSmsCode)
        {
            getSmsCode();
        }
    }
    /**
     * 获取短信验证码
     */
    private void getSmsCode() {
        String mobile = mDataBinding.edtMobileNum.getText().toString();
        WeakReference<Context> weakReferenceContext = new WeakReference<>(ModifyNamIdActivity.this);
        //手机号格式检查
        boolean isCorrect = CommonUtil.checkMobile(mobile,weakReferenceContext,true);
        if(isCorrect) {
            mViewModel.getSmsCode("","UPDATE_IDCARD");
        }
    }

    /**
     * 提交真实姓名
     */
    public void updateName() {
        mPersonName = mDataBinding.edtPersonName.getText().toString();
        if(!TextUtils.isEmpty(mPersonName)) {
            mViewModel.updateUserInfo("",mPersonName,"","","");
        }
        else{
            Toast.makeText(ModifyNamIdActivity.this,R.string.person_name_emptey,Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 提交身份证
     */
    public void updateIdentify() {
        mIdCard = mDataBinding.edtIdentify.getText().toString();
        mSmsCode = mDataBinding.edtSmsCode.getText().toString();
        boolean isCorrect = checkInput();
        if(isCorrect){
            mViewModel.updateUserInfo(mIdCard,"","","",mSmsCode);
        }
    }

    /**
     * 检查输入是否正确
     * @return
     */
    public boolean checkInput() {
        WeakReference<Context> weakReferenceContext = new WeakReference<>(ModifyNamIdActivity.this);
        //身份证号
        boolean isCorrect = true;
        if(isCorrect) {
            isCorrect = CommonUtil.checkIdentify(mIdCard,weakReferenceContext);
        }
        return isCorrect;
    }
}